//
//  JWCalendarListController.m
//  SiqiCalendar
//
//  Created by sunjiawen on 2018/4/9.
//  Copyright © 2018年 sjw. All rights reserved.
//

#import "JWCalendarListController.h"
#import "JWCalendarListViewModel.h"
#import "JWCalendarListCell.h"
#import <ChameleonFramework/Chameleon.h>
#import "JWCalendarModel.h"
#import "JWCalendarListEditTableViewController.h"
#import "UIViewController+LGSideMenuController.h"
#import "JWCalendarSettingHelper.h"
#import "JWSettingCalendarNameViewController.h"

static NSString * const kCalendarListCellID = @"CalendarListCellID";

@interface JWCalendarListController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIView *customNavView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) JWCalendarListViewModel *viewModel;
@end

@implementation JWCalendarListController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    [self loadData];
}

#pragma mark - private
- (void)initialData {
    
}

- (void)setupUI {
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([JWCalendarListCell class]) bundle:nil] forCellReuseIdentifier:kCalendarListCellID];
    
    self.tableView.tableFooterView = ({
        UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 75)];
        footerView.backgroundColor = [UIColor whiteColor];
        footerView.userInteractionEnabled = YES;
        
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_add_wx"]];
        imageView.frame = CGRectMake(10, 10, 55, 55);
        imageView.userInteractionEnabled = YES;
        [footerView addSubview:imageView];
        
        UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) + 10, 0, 150, footerView.frame.size.height)];
        textLabel.text = @"创建新的日历";
        textLabel.textColor = [UIColor colorFromHexString:@"#d7d7d7"];
        textLabel.textAlignment = NSTextAlignmentLeft;
        textLabel.font = [UIFont boldSystemFontOfSize:20];
        [footerView addSubview:textLabel];
        
        UIView *bottomLine = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(footerView.frame)-1, CGRectGetWidth(footerView.frame), 1)];
        bottomLine.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.1];
        [footerView addSubview:bottomLine];
        
        UITapGestureRecognizer *footerViewTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(footerViewTapAction:)];
        [footerView addGestureRecognizer:footerViewTapGesture];
        
        footerView;
    });
}

- (void)loadData {
    
    [self.viewModel loadDataWithBlock:^(NSArray *list, CommonError *error) {
        [self.dataSource removeAllObjects];
        [self.dataSource addObjectsFromArray:list];
        
        [self.tableView reloadData];
    }];
}

#pragma mark - table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    JWCalendarListCell *cell = [tableView dequeueReusableCellWithIdentifier:kCalendarListCellID];
    JWCalendarModel *model = self.dataSource[indexPath.row];
    cell.headImageView.backgroundColor = [UIColor randomFlatColor];
    cell.titleLabel.text = model.calendarName;
    
    return cell;
}

#pragma mark - table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    JWCalendarModel *model = self.dataSource[indexPath.row];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:JWCalendarListDidSelectedNotification object:model.calendarId];// 通知刷新主界面
    
    [self.sideMenuController hideLeftViewAnimated:YES completionHandler:nil];
}

#pragma mark - table view's scroll view delegate

#pragma mark - Action
- (IBAction)editCalendarListBtnAction:(UIButton *)sender {
    JWCalendarListEditTableViewController *vc = [[JWCalendarListEditTableViewController alloc]initWithNibName:NSStringFromClass([JWCalendarListEditTableViewController class]) bundle:nil];
    
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark - 创建日历
/**
 创建新日历
 
 @param tap 点击手势
 */
- (void)footerViewTapAction:(UITapGestureRecognizer *)tap {
    // 新建时 edit 置为 false
    [JWCalendarSettingHelper sharedInstance].edit = NO;
    
    // 新建一个全局的待装载的模型
    [[JWCalendarSettingHelper sharedInstance] setupInitialCalendarModel];
    
    
    JWSettingCalendarNameViewController *vc = [[JWSettingCalendarNameViewController alloc]initWithNibName:NSStringFromClass([JWSettingCalendarNameViewController class]) bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark - Getter
- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (JWCalendarListViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [[JWCalendarListViewModel alloc]init];
    }
    return _viewModel;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

@end
